package cn.bdqn.controller;

import cn.bdqn.domain.Replenish;
import cn.bdqn.domain.ReplenishEquip;
import cn.bdqn.service.ReplenishEquipService;
import cn.bdqn.service.ReplenishService;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 补货单配货控制层
 * @author dkj
 */
@RestController
public class ReplenishEquipController {


    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ReplenishEquipService replenishEquipService;


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/replenishEquip/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){
        try {
            logger.info("进入了,参数：id={}",id);
            //根据id查询
            ReplenishEquip replenishEquip = replenishEquipService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(replenishEquip);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }


    /**
     * 根据补货单号查询
     * @param replenishId
     * @return
     */
    @GetMapping("/replenishEquip/queryByReplenishId/{replenishId}")
    public Result queryByReplenishId(@PathVariable("replenishId") Integer replenishId){

        try {
            logger.info("进入了，参数：replenishId={}",replenishId);
            //根据补货单id查询
            List<ReplenishEquip> replenishEquips = replenishEquipService.queryByReplenishId(replenishId);
            //返回结果
            return ResultGenerator.genSuccessResult(replenishEquips);

        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }


    /**
     * 新增
     * @param replenishEquips
     * @return
     */
    @PostMapping("/replenishEquip/save/")
    public Result saveReplenishEquip(@RequestBody List<ReplenishEquip> replenishEquips){
        try {

            logger.info("进入了，参数：replenishEquip={}",replenishEquips);
            //新增
            List<ReplenishEquip> replenishEquipList = replenishEquipService.saveReplenishEquip(replenishEquips);
            //返回结果
            return ResultGenerator.genSuccessResult(replenishEquipList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }


    /**
     * 修改
     * @param replenishEquip
     */
    @PutMapping("/replenishEquip/update/")
    public Result updateReplenishEquip(@RequestBody ReplenishEquip replenishEquip){
        try {
            logger.info("进入了，参数：replenishEquip={}",replenishEquip);
            //更新
            replenishEquipService.updateReplenishEquip(replenishEquip);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("报错了");
        }
    }
}
